package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.common.util.AliYunOSSOperators;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.UUID;

@Slf4j
@RestController
public class FileHandleController {
    @Autowired
    private AliYunOSSOperators oss;

    @PostMapping("/upload")
    public Result upload(@RequestParam("image") MultipartFile image) {
        log.info("上传文件：{}", image);
        try {
            // 参数校验
            Objects.requireNonNull(image, "文件文能为空");
            Objects.requireNonNull(image.getOriginalFilename(), "文件名不能为空");
            return Result.success(oss.upload(image.getBytes(), UUID.randomUUID()+ image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."))));
        } catch (Exception e) {
            log.error("上传文件失败：{}", e.getMessage());
            return Result.error("上传失败");
        }
    }
}

/*
@Slf4j
@RestController
public class FileHandleController {
    @PostMapping("/upload")
    //@RequestParam("abc") MultipartFile file  --->  @RequestParam("abc")的作用是把upload.html中name为abc提取出来，并赋给file变量
    public String upload(String username, Integer age,MultipartFile file) throws IOException {
        log.info("上传文件开始...{}, {}, {}", username, age, file);

        //获取文件信息
        System.out.println(file.getName());
        //获取文件名
        System.out.println(file.getOriginalFilename());
        //获取文件大小(字节)
        System.out.println(file.getSize());
        //获取文件类型
        System.out.println(file.getContentType());
        //获取文件字节数组
        System.out.println(file.getBytes());
        //判断文件是否为空
        System.out.println(file.isEmpty());
        //获取文件输入流
        System.out.println(file.getResource());
        //保存文件 到指定位置
        file.transferTo(new File("D:/Project/BackEndCode/qk-parent/" + file.getOriginalFilename()));
        return "上传成功";
    }
}
*/
